package top.deepdesigner.controller.statistics;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.deepdesigner.annotation.RequiresPermissionsDesc;
import top.deepdesigner.service.DtsGoodsService;
import top.deepdesigner.util.ResponseUtil;
import top.deepdesigner.vo.StatGoodVO;
import top.deepdesigner.vo.StatVo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/24 星期六 09:37:58 <br/>
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/stat")
public class GoodsStatController {
  public static final int DAYS = 30;
  @Autowired
  private DtsGoodsService goodsService;


  @RequiresPermissionsDesc(menu = {"统计", " 商品统计"}, button = "获取列表")
  @RequiresPermissions(value = {"admin:stat:goods"})
  @GetMapping("/goods")
  public Object list() {
    StatVo goodsStatVo = new StatVo();

    List<StatGoodVO> orderByDate = goodsService.getGoodsByDate(DAYS);
    List<Map> collect = orderByDate.stream().map(item -> {
      HashMap<String, Object> map = new HashMap<>();
      map.put("day", item.getDay());
      map.put("orders", item.getOrders());
      map.put("products", item.getProducts());
      map.put("amount", item.getAmount());
      return map;
    }).sorted((o1, o2) -> {
      String day1 = (String) o1.get("day");
      String day2 = (String) o2.get("day");
      return day1.compareTo(day2);
    }).collect(Collectors.toList());

    String[] columns = {"day", "orders", "products", "amount"};
    goodsStatVo.setColumns(columns);
    goodsStatVo.setRows(collect);
    return ResponseUtil.ok(goodsStatVo);
  }
}
